package com.maple.common.utils.agreements;
import com.maple.common.utils.Tools;
import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class B6AgreementUtil {
    /**
     * 截取字符串
     * */
    public static void testRegexp(String s,CallBackWeight callBackWeight) {
        if (s != null && !"".equals(s)) {
            String regex = "FF[0-9a-fA-F]{2}[0-9]{6}";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(s);
            while (matcher.find()) {
                String matcherString = matcher.group();
                System.out.println(matcherString);
                byte[]data=Tools.parseHexStr2Byte(matcherString.substring(0,4));
                int dian=data[1]&7;
//                System.out.println(data[2]&4);//B状态 bit2
//                System.out.println(data[1]&7-2);//小数点位
                /**状态字B校验**/
                boolean isJingzhong=(data[1]&8)==0;//是否是净重
                boolean isFu=(data[1]&32)!=0;//单位是否为负
                boolean isWending=(data[1]&64)==0;//是否稳定
                boolean isKG=(data[1]&16)==0;//单位
                boolean isYichu=(data[1]&128)!=0;//溢出
                System.out.println("净重："+isJingzhong+",负："+isFu+",稳定："+isWending+",kg："+isKG+",溢出:"+isYichu);
                if (isFu||!isWending||isYichu){
                    continue;
                }
                /**获取重量*/
                String weight=matcherString.substring(4);
                callBackWeight.succeed(getData(dian,weight));;

            }
        }
    }
    public static String getData(int dian,String data){
        if (data.length()!=6){
            return "";
        }
        StringBuilder weight = new StringBuilder();
        for (int i = data.length()-1; i >=0; i--) {
            if (i%2==0){
                String a=data.substring(i,i+2);
                weight.append(a);
            }
        }
        weight.insert(weight.length()-dian,".");
        try {
            BigDecimal decimal=new BigDecimal(weight.toString().trim());
            System.out.println(decimal.toString());
            return decimal.toString();
        }catch (NumberFormatException e){
            System.out.println("数据解析错误");
            return "";
        }

    }
}
